<?php

App::uses('Crud', 'Model');
App::uses('Category', 'Model');

class ShiftModel extends Crud {

    /**
     * Al Agregar/eliminar/actualizar las constantes de los modelos implica ir
     * y actualizar los comentarios en los campos de la tabla asociada a este modelo.
     */
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;

    public function getByUserAndLineAndWorkdate($userId, $lineId, $workdate)
    {
        $filters = array(
            'fields' => array(
                'ShiftModel.id as id',
                'u.id as u',
                'u.full_name',
                'u.nomina as nom',
                'u.curp',
                'ShiftModel.line_id',
                'ShiftModel.shift_id',
                'ShiftModel.assist_id',
                'l.colour as lc',
            ),
            'joins' => array(
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'type' => 'inner',
                    'conditions' => array(
                        'u.id = ShiftModel.user_id',
                    ),
                ),
                array(
                    'table' => 'lines',
                    'alias' => 'l',
                    'type' => 'inner',
                    'conditions' => array(
                        'l.id = ShiftModel.line_id',
                    ),
                ),
            ),
            'conditions' => array(
                'user_id' => $userId,
                'line_id' => $lineId,
                'work_date' => $workdate,
            ),
        );
        return $this->find('first', $filters);
    }


    public function createRol($date, $defaultLine = '54c7dee0-9ff0-4780-9906-12c41b09678d', $defaultShift = 1, $defaultAssist = 1)
    {
        $queryString = '
        insert into shift_models (id, user_id, shift_id, assist_id, line_id, work_date)
        select
            UUID(),
            u.id,
            IFNULL(sm.shift_id, IFNULL(sm2.shift_id, ?)),
            ?,
            IFNULL(sm.line_id, IFNULL(sm2.line_id, ?)),
            ?
        from us3rs_r4gg14n4 u
        left join shift_models sm on sm.user_id = u.id and sm.work_date = ?
        left join shift_models sm2 on sm2.user_id = u.id and sm2.work_date = DATE_SUB(?, INTERVAL 1 day)
        where u.status = 1 and sm.id is null and u.category_id = ?';
        $db = $this->getDataSource();
        $db->fetchAll($queryString, array(
            $defaultShift,
            $defaultAssist,
            $defaultLine,
            $date,
            $date,
            $date,
            Category::ASOCIADO,
        ));
    }

    /**
     * Actualizamos el turno en el rol de turnos para todos los miembros de un equipo
     * @param $teamId Id del equipo
     */
    public function updateShiftByTeam($teamId)
    {
        $queryString = '
        update shift_models sm
        inner join team_users tu ON sm.user_id = tu.user_id
        inner join teams t on t.id = tu.team_id
        set sm.shift_id = t.shift_id
        where tu.team_id = ?
        and sm.work_date >= DATE(now())
        and t.shift_id IS NOT NULL
        ';
        $db = $this->getDataSource();
        $db->fetchAll($queryString, array($teamId));
    }

}
